package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl.user;

import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.common.PagerEntity;
import com.alibaba.algo.cooperation.feign.service.RoleFeignService;
import com.alibaba.algo.cooperation.feign.vo.req.FeignSelectRoleListRequestVO;
import com.alibaba.algo.cooperation.feign.vo.req.GetRoleBaseInfoReqVO;
import com.alibaba.algo.cooperation.feign.vo.req.GetRoleUserReqVO;
import com.alibaba.algo.cooperation.feign.vo.req.SelectRoleUserReqVO;
import com.alibaba.algo.cooperation.feign.vo.res.FeignRoleListResponseVO;
import com.alibaba.algo.cooperation.feign.vo.res.RoleBaseInfoResponseVO;
import com.alibaba.algo.cooperation.feign.vo.res.RoleUserInfoResVO;
import com.alibaba.algo.cooperation.feign.vo.res.UserInfoResVO;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.enums.SourceApplicationEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.UserRoleService;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.StringUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ResponseResult;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.userentity.RoleInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.userentity.UserInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description 用户角色服务实现方法
 * @author liujinjian
 * @date 2021-11-01
 * @version 1.0
 */
@Service
public class UserRoleServiceImpl implements UserRoleService {

    @Autowired
    private RoleFeignService roleFeignService;

    /**
     * 业务来源应用编码
     */
    @Value("${task.sourceAppCode:mes2}")
    private String sourceAppCode;

    /**
     * 通过角色ID查询用户列表
     * @param roleCode
     * @return
     */
    @Override
    public List<UserInfo> getUserListByRoleId(String roleCode) {
        List<UserInfo> userInfoList = new ArrayList<UserInfo>();
        SelectRoleUserReqVO reqVO = new SelectRoleUserReqVO();
        reqVO.setRoleId(Long.valueOf(roleCode));
        BaseResult<List<RoleUserInfoResVO>> dataList = roleFeignService.qryUserListByRoleId(reqVO);
        for (RoleUserInfoResVO vo : dataList.getData()) {
            UserInfo entity = new UserInfo();
            entity.setUserCode(vo.getUserId().toString());
            entity.setUserName(vo.getUserName());
            userInfoList.add(entity);
        }

        return userInfoList;
    }

    @Override
    public RoleInfo getRoleInfoByCode(String code) {
        FeignSelectRoleListRequestVO requestVO = new FeignSelectRoleListRequestVO();
        requestVO.setSearchKey(code);
        requestVO.setPageIndex(1);
        requestVO.setPageSize(1);
        BaseResult<PagerEntity<FeignRoleListResponseVO>> result = roleFeignService.selectPage(requestVO);
        if(Objects.isNull(result)) {
            return null;
        }

        PagerEntity<FeignRoleListResponseVO> pagerEntityData = result.getData();
        if(Objects.isNull(pagerEntityData)) {
            return null;
        }

        List<FeignRoleListResponseVO> voList = pagerEntityData.getDataList();
        if(CollectionUtils.isEmpty(voList)) {
            return null;
        }

        FeignRoleListResponseVO roleVo = voList.get(0);
        RoleInfo roleInfo = new RoleInfo();
        roleInfo.setRoleCode(String.valueOf(roleVo.getId()));
        roleInfo.setRoleName(roleVo.getRoleName());
        roleInfo.setCode(roleVo.getRoleCode());
        return roleInfo;
    }

    @Override
    public List<UserInfo> getUserListByRoleIdList(List<String> roleCodes) {
        List<UserInfo> userInfoList = new ArrayList<UserInfo>();
        if(CollectionUtils.isNotEmpty(roleCodes)) {
            List<Long> roleCodeList = roleCodes.stream().map(roleCode -> {
                return Long.parseLong(roleCode);
            }).collect(Collectors.toList());
            GetRoleUserReqVO reqVo = new GetRoleUserReqVO();
            reqVo.setRoleIdList(roleCodeList);
            BaseResult<List<UserInfoResVO>> dataList = roleFeignService.qryUserListByRole(reqVo);
            if(Objects.isNull(dataList)) {
                return Collections.emptyList();
            }

            List<UserInfoResVO> userInfoResVos = dataList.getData();
            if(CollectionUtils.isEmpty(userInfoResVos)) {
                return Collections.emptyList();
            }

            for (UserInfoResVO vo : userInfoResVos) {
                UserInfo entity = new UserInfo();
                entity.setUserCode(String.valueOf(vo.getUserId()));
                entity.setUserName(vo.getUserName());
                userInfoList.add(entity);
            }
        }
        return userInfoList;
    }

    /**
     * 根据角色名称获取查询角色列表
     *
     * @param roleName
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public ResponseResult getUserRolePageList(String roleName, Integer pageNo, Integer pageSize) {
        List<RoleInfo> roleInfoList = new ArrayList<>();
        FeignSelectRoleListRequestVO vo = new FeignSelectRoleListRequestVO();
        if (StringUtils.isNotEmpty(roleName) && StringUtils.isNotBlank(roleName)) {
            vo.setRoleName(roleName);
        }
        vo.setPageIndex(pageNo);
        vo.setPageSize(pageSize);
        Long total = 0l;
        BaseResult<PagerEntity<FeignRoleListResponseVO>> roleList = roleFeignService.selectPage(vo);
        if (roleList!=null && roleList.getData()!=null && roleList.getData().getDataList()!=null) {
            total = roleList.getData().getTotal();
            for (FeignRoleListResponseVO fvo : roleList.getData().getDataList()) {
                RoleInfo entity = new RoleInfo();
                entity.setRoleCode(fvo.getId().toString());
                entity.setRoleName(fvo.getRoleName());
                roleInfoList.add(entity);
            }
        }
        return ResponseResult.success(roleInfoList)
                .setTotalCount(total.intValue())
                .setPageNum(pageNo)
                .setPageSize(pageSize);
    }

    @Override
    public RoleInfo getRoleInfoByRoleCode(String roleId, String roleCode, String isDeleted) {
        GetRoleBaseInfoReqVO requestVO = new GetRoleBaseInfoReqVO();
        // 不为空作为查询条件
        if(StringUtils.isNotBlank(sourceAppCode)) {
            String appCode = "";
            if(Objects.equals(sourceAppCode,SourceApplicationEnum.bom2.name())) {
                appCode = SourceApplicationEnum.bom2.getCode();
            }else if(Objects.equals(sourceAppCode,SourceApplicationEnum.mes2.name())) {
                appCode = SourceApplicationEnum.mes2.getCode();
            }
            requestVO.setAppCode(appCode);
        }
        if(StringUtils.isNotBlank(roleId)) {
            requestVO.setRoleId(Long.parseLong(roleId));
        }
        if(StringUtils.isNotBlank(roleCode)) {
            requestVO.setRoleCode(roleCode);
        }
        if(StringUtils.isNotBlank(isDeleted)) {
            requestVO.setIsDeleted(isDeleted);
        }
        BaseResult<RoleBaseInfoResponseVO> baseResult = roleFeignService.getRoleBaseInfoByIdOrCode(requestVO);
        if(Objects.isNull(baseResult)) {
            return null;
        }

        // 响应数据
        RoleBaseInfoResponseVO responseData = baseResult.getData();
        if(Objects.isNull(responseData)) {
            return null;
        }

        // 组装角色对象信息
        RoleInfo roleInfo = new RoleInfo();
        roleInfo.setRoleCode(String.valueOf(responseData.getRoleId()));
        roleInfo.setRoleName(responseData.getRoleName());
        roleInfo.setCode(responseData.getRoleCode());
        return roleInfo;
    }

}